package com.utils.regex;

import java.util.regex.Pattern;

public class RegexUtils {
    private static final String REGEX_NUM = ".*[0-9].*"; //  ".*\\d.*"
    private static final String REGEX_UP_LETTER = ".*[A-Z].*";
    private static final String REGEX_LOW_LETTER = ".*[a-z].*";
    private static final String REGEX_SPECIAL_CHARACTER = ".*[^0-9a-zA-Z].*";
    private static final String REGEX_CHINESE = ".*[\\u4e00-\\u9fa5].*";
    private static final String REGEX_DATETIME = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|([1][0-9])|([2][0-4]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";
    private static final String REGEX_DATE = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))?$";
    private static final String REGEX_TIME = "((((0?[0-9])|([1][0-9])|([2][0-4]))\\:([0-5]?[0-9])((\\s)|(\\:([0-5]?[0-9])))))?$";

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        String data = "1988-12-31 12:45:12";
        boolean matches = isDateTime(data);
        System.out.println(matches);
        System.out.println(System.currentTimeMillis() - start);

//        Pattern compile = Pattern.compile(pattern);
//        Matcher matcher = compile.matcher(data);
//        if (matcher.find()) {
//            System.out.println(matcher.group(0));
//            System.out.println(matcher.groupCount());
//        }

    }

    public static boolean haveNumber(String data) {
        return Pattern.matches(REGEX_NUM, data);
    }

    public static boolean haveUpLetter(String data) {
        return Pattern.matches(REGEX_UP_LETTER, data);
    }

    public static boolean haveLowLetter(String data) {
        return Pattern.matches(REGEX_LOW_LETTER, data);
    }

    public static boolean haveSpecialCharacter(String data) {
        return Pattern.matches(REGEX_SPECIAL_CHARACTER, data);
    }

    public static boolean haveChinese(String data) {
        return Pattern.matches(REGEX_CHINESE, data);
    }

    /**
     * YYYY-MM-DD HH:mm:ss
     */
    public static boolean isDateTime(String data) {
        return Pattern.matches(REGEX_DATETIME, data);
    }
    /**
     * YYYY-MM-DD
     */
    public static boolean isDate(String data) {
        return Pattern.matches(REGEX_DATE, data);
    }
    /**
     * HH:mm:ss
     */
    public static boolean isTime(String data) {
        return Pattern.matches(REGEX_TIME, data);
    }
}
